/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.guias.data;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Joshua
 */
public class Ejecutar extends Conexion {
    
    
    
    public ArrayList ejecutarSQLselect (String pSql, boolean nombre) throws SQLException{
        try{
            CrearConexion();
            ArrayList la_arreglo = new ArrayList();
            pstm = (PreparedStatement) conn.prepareStatement(pSql);
            rst = pstm.executeQuery();
            int pNumcam = rst.getMetaData().getColumnCount();
            if(nombre)
            {
                ArrayList la_campos = new ArrayList();
                for(int j = 1; j <= pNumcam; j++)
                    la_campos.add(rst.getMetaData().getColumnName(j));

                la_arreglo.add(la_campos);
            }
            ArrayList la_campos;
            for(; rst.next(); la_arreglo.add(la_campos))
            {
                la_campos = new ArrayList();
                for(int i = 1; i <= pNumcam; i++)
                    if(rst.getString(i) == null)
                        la_campos.add("");
                    else
                        la_campos.add(rst.getString(i));

            }

            rst.close();
            if(la_arreglo.size() > 0)
            {
                ArrayList arraylist = la_arreglo;
                CerrarConexion();
                return arraylist;
            } else
            {
                ArrayList arraylist1 = null;
                CerrarConexion();
                return arraylist1;
            }
        }
        catch(Exception err){
            err.printStackTrace();
            String sql_insert_error = "insert into tb_logs (id_log, id_punto, mensaje, fecha, id_registro) values ((SELECT coalesce(lpad(MAX(to_number(id_log))+1||'',8,'00000000'),'00000001') as MAX FROM tb_logs ), '0-0', '3:" + err.getMessage() + "',sysdate,'0-0')";
            ejecutarSQL(sql_insert_error);
            return null;
        }
        
    }
    
    public int ejecutarSQL(String sql){
        int retorno = 0;
        try {
                super.CrearConexion();
                Statement stmt = (Statement) conn.createStatement();
                retorno = stmt.executeUpdate(sql);
        } catch (Exception e) {
                retorno = -1;
                e.printStackTrace();
        }finally{
                try {
                    super.CerrarConexion();
                } catch (SQLException e) {
                    retorno = -2;
                    e.printStackTrace();
                }
        }

        return retorno;
    }

    public Object ejecutarSQLescalar(String pSql) throws SQLException {
        Object objRetorno = null;
        try {
            CrearConexion();
            
            pstm = (PreparedStatement) conn.prepareStatement(pSql);
            rst = pstm.executeQuery();
            rst.next();
            objRetorno = rst.getObject(1);
            rst.close();
            
        } catch (Exception err) {
            String sql_insert_error =
                "insert into tb_logs (id_log, id_punto, mensaje, fecha, id_registro) values ((SELECT coalesce(lpad(MAX(to_number(id_log))+1||'',8,'00000000'),'00000001') as MAX FROM tb_logs ), '0-0', '3[ejecutarSQLescalar]:" +
                err.getMessage() + "',sysdate,'0-0')";
            ejecutarSQL(sql_insert_error);
            return null;
        }
        return objRetorno;
    }

}
